package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class InstanceOperation {
    /*
    public static void main(String[] args) throws Exception{
        Class<?> aClass = Class.forName("reflect.Dog");
        Object o = aClass.newInstance();
        Dog dog =(Dog)o;
        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(dog,"牛逼");
        System.out.println(dog.getName());
    }*/

    /**
     * setAccessible()在方法上的使用
     * @param args
     * @throws Exception
     */
   /* public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("reflect.Dog");
        Dog dog = (Dog) aClass.newInstance();
        Method privateMethod = aClass.getDeclaredMethod("privateMethod");
        privateMethod.setAccessible(true);
        privateMethod.invoke(dog,null);
        Method privateMethod2 = aClass.getDeclaredMethod("privateMethod2", String.class);
        privateMethod2.setAccessible(true);
        privateMethod2.invoke(dog,"牛逼");
    }*/

    /**
     * 操作构造方法
     * @param args
     */
    public static void main(String[] args)throws Exception {
        Class<?> aClass = Class.forName("reflect.Dog");
        Constructor<?> constructor = aClass.getConstructor(int.class, String.class, int.class);
        System.out.println(constructor);
        Dog dog = (Dog)constructor.newInstance(1, "sss", 20);
        System.out.println(dog.toString());
    }
}
